package me.vbdetlevvb.plugins.bananasphere;

import java.util.Random;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;

public class SpherePopulator extends BlockPopulator {
	private static final int CHANCE = 5; // Out of 100
	private static final int MIN_SIZE = 8;
	private static final int MAX_SIZE = 16;

	@Override
    public void populate(World world, Random random, Chunk source) {
        if (random.nextInt(100) <= CHANCE) {
			int x = (source.getX() << 4) + random.nextInt(16);
			int z = (source.getZ() << 4) + random.nextInt(16);
			int y = 64;
			Vector center = new BlockVector(x, y, z);
			int radius = random.nextInt(MAX_SIZE - MIN_SIZE) + MIN_SIZE;

			for (int dx = -radius; dx <= radius; dx++) {
				for (int dz = -radius; dz <= radius; dz++) {
					for (int dy = -radius; dy <= radius; dy++) {
						double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
						if (dy <= 0) {
							if (distance <= radius + 0.5) {
								world.getBlockAt(x + dx, y + dy, z + dz).setType(Material.STONE);
							}
						} else if (distance > radius - 0.5 && distance <= radius + 0.5) {
							world.getBlockAt(x + dx, y + dy, z + dz).setType(Material.GLASS);
						}
					}
				}
			}
		}
    }
}
